package com.yangff.controller;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangff.anno.LogExecuteTime;
import com.yangff.entity.product.entity.Financialproduct;
import com.yangff.entity.product.entity.Fundnetvalue;
import com.yangff.entity.product.entity.dto.FundnetvalueDTO;
import com.yangff.entity.product.entity.vo.FundnetvalueVo;
import com.yangff.entity.vo.ResultVo;
import com.yangff.service.IFinancialproductService;
import com.yangff.service.IFundnetvalueService;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 存储基金每日净值信息 前端控制器
 * </p>
 *
 * @author yangff
 * @since 2025-01-15
 */
@LogExecuteTime
@RestController
@RequestMapping("/fundnetvalue")
@RequiredArgsConstructor
@Slf4j
public class FundnetvalueController {

    private final IFundnetvalueService fundnetvalueService;


    //按照代码查询相应记录
    @Cacheable(value = "fundnetvalue", key = "#code")
    @GetMapping("/{code}")
    public List<FundnetvalueVo> getFundNetValueByCode(@PathVariable String code) {
        List<Fundnetvalue> fundnetvalues = fundnetvalueService.list(new LambdaQueryWrapper<Fundnetvalue>()
                .eq(Fundnetvalue::getCode, code));

        //log.info("list结果如下：{}",fundnetvalues);
        List<FundnetvalueVo> collect = BeanUtil.copyToList(fundnetvalues, FundnetvalueVo.class);
        return collect;
    }



    @CacheEvict(value = "fundnetvalue", key = "#code")
    @DeleteMapping("/{code}")
    public ResultVo deleteFundNetValueByCode(@PathVariable String code){

        fundnetvalueService.remove(new LambdaQueryWrapper<Fundnetvalue>().eq(Fundnetvalue::getCode,code));
        return ResultVo.success();
    }


    //所有基金更新下一天最新的基金净值记录，传入空参数

    @PostMapping("/update")
    public ResultVo updateFundNetValue(@RequestBody ResultVo empty){
        fundnetvalueService.updateFundNetValue();
        return ResultVo.success();
    }





}
